<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:dg="livcos.org/ns/doc/diagram"
	xmlns:ddl="livcos.org/ns/db/ddl"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="doc ddl dg f xs xlink">
	
	<xsl:import href="../../../../../base.xsl"/>
	
	<xsl:param name="detailsView" select="'/livcos.org/web/doc'"/>
	
	<xsl:variable name="rowHeight" select="12"/>
	
	<xsl:template match="ddl:table">
		<xsl:variable name="tmp">
			<xsl:apply-templates select="." mode="prep"/>
		</xsl:variable>
		<xsl:apply-templates select="$tmp/*"/>
	</xsl:template>
	
	<xsl:template match="ddl:table" mode="prep">
		<table id="{f:object-id(.)}" xmlns="">
			<title><xsl:value-of select="@name"/></title>
			<xsl:apply-templates select="ddl:field" mode="#current"/>
		</table>
	</xsl:template>
	
	<xsl:template match="ddl:field" mode="prep">
		<row xmlns="">
			<xsl:copy-of select="@nullable[. = '1']"/>
			<label>
				<xsl:value-of select="@name"/>
			</label>
			<label>
				<xsl:value-of select="@dbtype"/>
				<xsl:if test="@type = 'VARCHAR'">(<xsl:value-of select="@size"/>)</xsl:if>
			</label>
			<xsl:variable name="pk" select="../ddl:key[ddl:field/@ref-name = current()/@name][1]"/>
			<comment>
				<xsl:value-of select="@name"/>
				<xsl:text>: </xsl:text>
				<xsl:value-of select="@dbtype"/>
				<xsl:if test="@type = 'VARCHAR'">(<xsl:value-of select="@size"/>)</xsl:if>
				<xsl:if test="@nullable = '1'">; Allow NULL</xsl:if>
				<xsl:if test="$pk">; primary key</xsl:if>
			</comment>
			<xsl:if test="$pk">
				<key/>
			</xsl:if>
			<xsl:variable name="refKey" select="../ddl:ref[ddl:field/@ref-name = current()/@name][1]"/>
			<xsl:if test="$refKey">
				<ref id="{f:entity-id($refKey/@id)}" ref="{$refKey/@key-table}"/>
			</xsl:if>
		</row>
	</xsl:template>
	
	<xsl:template match="table">
		<xsl:variable name="inclRows" select="not(@level) or (@level > 1)"/>
		<xsl:variable name="titleWidth" select="string-length(title) * 7 + 18"/>
		<xsl:variable name="maxRowWidth"
			select="max(for $n in row return string-length($n/label[1]) * 6 + string-length($n/label[2]) * 4.8) + 18"/>
		<xsl:variable name="w"
			select="if ($inclRows) then max(($titleWidth, $maxRowWidth)) else $titleWidth"/>
		<xsl:variable name="h" select="if ($inclRows) then count(row) * $rowHeight + 24 else 24"/>
		<xsl:variable name="c" select="$w div 2, $h div 2"/>
		<dg:box width="{$w}" height="{$h}" ref="{@id}">
			<g fill="#FFFFEE">
				<a xlink:href="{f:caller-uri(concat($detailsView, @id, '.html'))}" xlink:title="click for details">
					<rect fill="#EEEECC" stroke="black" x="0" y="0" width="{$w}" height="{$h}" rx="1" ry="1">
						<title><xsl:value-of select="title"/></title>
						<desc><xsl:value-of select="title"/></desc>
					</rect>
					<text x="{$c[1]}" y="16" fill="black" style="text-anchor: middle; font-size: 12px">
						<xsl:value-of select="title"/>
					</text>
				</a>
				<xsl:if test="row and $inclRows">
					<line x1="0" y1="22" x2="{$w}" y2="22" stroke="#556677" stroke-width="2px"/>
					<rect x="1" y="22" width="{$w - 2}" height="{$h - 23}" fill="#FFFFEE"/>
					<g transform="translate(6,19)">
						<xsl:apply-templates select="row">
							<xsl:sort select="key" order="descending"/>
							<xsl:sort select="lower-case(label[1])"/>
						</xsl:apply-templates>
					</g>
				</xsl:if>
			</g>
			<xsl:apply-templates select="row/ref"/>
		</dg:box>
	</xsl:template>
	
	<xsl:template match="row">
		<xsl:if test="key">
			<image x="-4" y="{position() * $rowHeight - 7}" width="5" height="9"
				xlink:href="{resolve-uri('../img/pk.png')}"/>
		</xsl:if>
		<text x="2" y="{position() * $rowHeight}"
			fill="{if (@nullable) then '#444444' else 'black'}"
			style="font-size: {$rowHeight - 2}px; font-style: {if (@nullable) then 'italic' else 'inherit'};"
			xlink:title="{comment}">
			<xsl:value-of select="label[1]"/>
			<xsl:text> </xsl:text>
			<tspan style="font-size: 7px">
				<xsl:value-of select="label[2]"/>
			</tspan>
		</text>
	</xsl:template>
	
	<xsl:template match="ref">
		<dg:connect ref="{@id}" end-ref="{f:abs-ref(@ref)}" marker-start="#bullet"/>
	</xsl:template>
	
</xsl:stylesheet>
